package com.sx.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.dfec.model.Msg;
import com.dfec.utils.DruidPlugin;


/**
 * Servlet implementation class WarningServlet
 * @param <T>
 */
public class SpecialWeatherService {
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	List<Object> list=null;
    private String sql ="";
    public List<Object> getSpecialWeather(String  position,int page, int size) throws SQLException{
    	list=new ArrayList<>();
    	int num=((page-1)*size);
    	sql="SELECT * from d_special_weather where detailed like'%"+position+"%' order by id desc limit "+num+" , "+size ;
    	
    	list.add(qr.query(sql, new  MapListHandler()));
    	return list;
    }
    
    public int upLoad(String  details,String phone,String fileName,String title) throws SQLException{
		sql="INSERT into d_special_weather  (detailed,img,up_person,up_time,title) VALUES('"+details+"','"+fileName+"','"+phone+"',now(),'"+title+"')";
			
		return qr.update(sql);
    }

	public	 String  saveImg(byte [] imgdata,String path,String suffix) throws IOException{
				
				String pname = System.currentTimeMillis() + suffix;
				File file = new File(path+ "\\"+pname);
		        FileOutputStream fos = null;
		      
				fos = new FileOutputStream(file);
				fos.write(imgdata);
				fos.flush();
				if (fos!=null) {
				 fos.close();
				}
				
				return file.getPath();
					
	 }

	public List<Object> searchSpecialWeather(String keyword,String position) throws SQLException {
    		sql="SELECT * from d_special_weather where detailed like'%"+keyword+"%' and detailed like'%"+position+"%' order by id desc ";
    		list=new ArrayList<>();
    		list.add(qr.query(sql, new  MapListHandler()));
    	return list;
	}
	}
